﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BIS.Portal.Core.Global;
using BIS.Portal.Modules.CMS.Entity;
using BIS.Portal.Modules.CMS.BO;
using BIS.Utility.Converter;

namespace BIS.Portal.Modules.CMS.Adm
{
    public partial class AdminCmsDisplayTypeDetailsControl : System.Web.UI.UserControl
    {
        private int DisplayTypeId
        {
            get
            {
                return Functions.GetInt(Request.QueryString[QueryStringKeys.DisplayTypeId]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Session properties
        public string SearchByKeyword
        {
            get { return (null != Session["SearchByKeyword"] ? Session["SearchByKeyword"].ToString() : ""); }
            set { Session["SearchByKeyword"] = value; }
        }
        public int SearchByCategoryId
        {
            get { return (null != Session["SearchByCategoryId"] ? int.Parse(Session["SearchByCategoryId"].ToString()) : -1); }
            set { Session["SearchByCategoryId"] = value; }
        }
        public int SearchByIsHot
        {
            get { return (null != Session["SearchByIsHot"] ? int.Parse(Session["SearchByIsHot"].ToString()) : -1); }
            set { Session["SearchByIsHot"] = value; }
        }
        public int SearchByStatus
        {
            get { return (null != Session["SearchByStatus"] ? int.Parse(Session["SearchByStatus"].ToString()) : -1); }
            set { Session["SearchByStatus"] = value; }
        }
        public DateTime SearchByDateStart
        {
            get { return (null != Session["SearchByDateStart"] ? DateTime.Parse(Session["SearchByDateStart"].ToString()) : DateTime.Parse("1900-01-01")); }
            set { Session["SearchByDateStart"] = value; }
        }
        public DateTime SearchByDateEnd
        {
            get { return (null != Session["SearchByDateEnd"] ? DateTime.Parse(Session["SearchByDateEnd"].ToString()) : DateTime.Parse("9999-12-31")); }
            set { Session["SearchByDateEnd"] = value; }
        }
        #endregion

        #region Save Data In  Form
        private void SaveFormDataIntoSession()
        {
            // 
            SearchByKeyword = txtKeyword.Text;
            SearchByIsHot = int.Parse(ddlIsHotArticle.SelectedItem.Value);
            SearchByStatus = int.Parse(ddlStatus.SelectedItem.Value);
            SearchByCategoryId = int.Parse(ddlCategoryName.SelectedItem.Value);
            if (!string.IsNullOrEmpty(txtFromDate.Text))
            {
                SearchByDateStart = DateTime.Parse(txtFromDate.Text);
                if (!string.IsNullOrEmpty(txtToDate.Text))
                {
                    SearchByDateEnd = DateTime.Parse(txtToDate.Text);
                }
                else
                {
                    SearchByDateEnd = DateTime.Parse("9999-12-31");
                }
            }
            else
            {
                SearchByDateStart = DateTime.Parse("1900-01-01");
                if (!string.IsNullOrEmpty(txtToDate.Text))
                {
                    SearchByDateEnd = DateTime.Parse(txtToDate.Text);
                }
                else
                {
                    SearchByDateEnd = DateTime.Parse("9999-12-31");
                }
            }

        }
        #endregion

        #region Bind Data GridView
        private void BindDataIntoGridView()
        {
            //
            int languageUseId = 0;
            try
            {
                languageUseId = PortalContext.LanguageInUseId;
            }
            catch (Exception ex)
            {

                Console.Write(ex.Message);
                languageUseId = 1;
            }
            IList<AdminCmsArticleEntity> list = AdminCmsArticleBO.SearchArticle(SearchByKeyword, SearchByCategoryId, SearchByIsHot, SearchByStatus, languageUseId, SearchByDateStart, SearchByDateEnd);
            grvArticle.DataSource = list;
            grvArticle.DataKeyNames = new string[] { "article_id" };
            grvArticle.DataBind();
        }
        #endregion

        #region Bind DropDrownList
        protected void BinDataDropDownListCategory()
        {
            int languageUseId = 0;
            try
            {
                languageUseId = PortalContext.LanguageInUseId;
            }
            catch (Exception ex)
            {

                Console.Write(ex.Message);
                languageUseId = 1;
            }

            IList<AdminCmsCategoryEntity> list = AdminCmsCategoryBO.GetByLanguageInUseId(languageUseId);
            IList<AdminCmsCategoryEntity> listAdminCategoryEntity = new List<AdminCmsCategoryEntity>();
            AdminListViewUtility.ParseList(0, ref list, ref listAdminCategoryEntity);

            // binding dropdownlist            
            listAdminCategoryEntity.Insert(0, new AdminCmsCategoryEntity()
            {
                Category_id = 0,
                Category_name = "---Toàn bộ---"
            });

            AdminListViewUtility.AddDotList(ref listAdminCategoryEntity);
            ddlCategoryName.Items.Clear();
            for (int i = 0; i < listAdminCategoryEntity.Count; i++)
            {
                ListItem item = new ListItem();
                item.Text = listAdminCategoryEntity[i].Category_name;
                item.Value = listAdminCategoryEntity[i].Category_id.ToString();
                if (listAdminCategoryEntity[i].Category_type == 3)
                {
                    item.Attributes.Add("disabled", "disabled");
                }
                ddlCategoryName.Items.Add(item);
            }
            //ddlCategoryName.DataSource = listAdminCategoryEntity;
            //ddlCategoryName.DataValueField = "category_id";
            //ddlCategoryName.DataTextField = "category_name";            
            //ddlCategoryName.DataBind();              
            //if (CategoryId != null)
            //{
            //    ddlCategoryName.SelectedValue = CategoryId.ToString();
            //}
        }

        #endregion

        #region Page's Action
        protected void grvArticle_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvArticle.PageIndex = e.NewPageIndex;
            BindDataIntoGridView();
        }

        protected void grvDisplaySpecial_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvDisplaySpecial.PageIndex = e.NewPageIndex;
        }
       
        #endregion

        #region Button's Action
        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }
        
        protected void btnRight_Click(object sender, EventArgs e)
        {

        }

        protected void btnLeft_Click(object sender, EventArgs e)
        {

        }
        #endregion
        
    }
}